import torch
import torch.nn as nn

class conv_block(nn.Module):
    def __init__(self, ch_in, ch_out):
        super(conv_block, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True),
            nn.Conv2d(ch_out, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        x = self.conv(x)
        return x


class resconv_block(nn.Module):
    def __init__(self, ch_in, ch_out):
        super(resconv_block, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True),
            nn.Conv2d(ch_out, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True)
        )
        self.Conv_1x1 = nn.Conv2d(ch_in, ch_out, kernel_size=1, stride=1, padding=0)

    def forward(self, x):
        residual = self.Conv_1x1(x)
        x = self.conv(x)

        return residual + x


class up_conv(nn.Module):
    def __init__(self, ch_in, ch_out):
        super(up_conv, self).__init__()
        self.up = nn.Sequential(
            nn.Upsample(scale_factor=2),
            nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        x = self.up(x)
        return x

class single_conv(nn.Module):
    def __init__(self, ch_in, ch_out):
        super(single_conv, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(ch_in, ch_out, kernel_size=3, stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        x = self.conv(x)
        return x

# 残差UNet
class ResU_Net(nn.Module):
    def __init__(self, img_ch=1, output_ch=1, filters=[16, 32, 64, 128, 256]):
        super(ResU_Net, self).__init__()

        self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)

        self.Conv1 = resconv_block(ch_in=img_ch, ch_out=filters[0])
        self.Conv2 = resconv_block(ch_in=filters[0], ch_out=filters[1])
        self.Conv3 = resconv_block(ch_in=filters[1], ch_out=filters[2])
        self.Conv4 = resconv_block(ch_in=filters[2], ch_out=filters[3])
        self.Conv5 = resconv_block(ch_in=filters[3], ch_out=filters[4])

        self.Up5 = up_conv(ch_in=filters[4], ch_out=filters[3])
        self.Up_conv5 = resconv_block(ch_in=filters[3] * 2, ch_out=filters[3])

        self.Up4 = up_conv(ch_in=filters[3], ch_out=filters[2])
        self.Up_conv4 = resconv_block(ch_in=filters[2] * 2, ch_out=filters[2])

        self.Up3 = up_conv(ch_in=filters[2], ch_out=filters[1])
        self.Up_conv3 = resconv_block(ch_in=filters[1] * 2, ch_out=filters[1])

        self.Up2 = up_conv(ch_in=filters[1], ch_out=filters[0])
        self.Up_conv2 = resconv_block(ch_in=filters[0] * 2, ch_out=filters[0])

        self.Conv_1x1 = nn.Conv2d(filters[0], output_ch, kernel_size=1, stride=1, padding=0)

        self.Upp5 = up_conv(ch_in=filters[4], ch_out=filters[3])
        self.Upp_conv5 = resconv_block(ch_in=filters[3] * 2, ch_out=filters[3])

        self.Upp4 = up_conv(ch_in=filters[3], ch_out=filters[2])
        self.Upp_conv4 = resconv_block(ch_in=filters[2] * 2, ch_out=filters[2])

        self.Upp3 = up_conv(ch_in=filters[2], ch_out=filters[1])
        self.Upp_conv3 = resconv_block(ch_in=filters[1] * 2, ch_out=filters[1])

        self.Upp2 = up_conv(ch_in=filters[1], ch_out=filters[0])
        self.Upp_conv2 = resconv_block(ch_in=filters[0] * 2, ch_out=filters[0])

        self.Conv_1x1p = nn.Conv2d(filters[0], output_ch, kernel_size=1, stride=1, padding=0)

        self.active = torch.nn.Sigmoid()

    def forward(self, x):
        x1 = self.Conv1(x)

        x2 = self.Maxpool(x1)
        x2 = self.Conv2(x2)

        x3 = self.Maxpool(x2)
        x3 = self.Conv3(x3)

        x4 = self.Maxpool(x3)
        x4 = self.Conv4(x4)

        x5 = self.Maxpool(x4)
        x5 = self.Conv5(x5)

        d5 = self.Up5(x5)
        d5 = torch.cat((x4, d5), dim=1)
        d5 = self.Up_conv5(d5)

        d4 = self.Up4(d5)
        d4 = torch.cat((x3, d4), dim=1)
        d4 = self.Up_conv4(d4)

        d3 = self.Up3(d4)
        d3 = torch.cat((x2, d3), dim=1)
        d3 = self.Up_conv3(d3)

        d2 = self.Up2(d3)
        d2 = torch.cat((x1, d2), dim=1)
        d2 = self.Up_conv2(d2)

        d1 = self.Conv_1x1(d2)
        outL = self.active(d1)

        d55 = self.Upp5(x5)
        d55 = torch.cat((x4, d55), dim=1)
        d55 = self.Upp_conv5(d55)

        d44 = self.Upp4(d55)
        d44 = torch.cat((x3, d44), dim=1)
        d44 = self.Upp_conv4(d44)

        d33 = self.Upp3(d44)
        d33 = torch.cat((x2, d33), dim=1)
        d33 = self.Upp_conv3(d33)

        d22 = self.Upp2(d33)
        d22 = torch.cat((x1, d22), dim=1)
        d22 = self.Upp_conv2(d22)

        d11 = self.Conv_1x1p(d22)
        outW = self.active(d11)

        return outL, outW


